package com.school.common.base;

import java.sql.SQLException;
import java.util.List;
import java.util.regex.Pattern;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * 
 * 数据访问对象基类
 * 
 * @author Administrator
 * 
 */

public class BaseDao extends HibernateDaoSupport {
	/**
	 * 说明：构造函数
	 */
	public BaseDao() {

	}

	/**
	 * 获得HIBERNATE的SESSION
	 * 
	 * @return
	 */

	public final Session getCurrentSession() {
		Session session = this.getSessionFactory().getCurrentSession();
		return session;
	}

	/**
	 * 删除对象
	 * 
	 * @param obj
	 */
	public void delete(Object obj) {
		this.getCurrentSession().delete(obj);
	}

	/**
	 * 更新对象
	 * 
	 * @param obj
	 */
	public void update(Object obj) {
		this.getCurrentSession().update(obj);
	}

	/**
	 * 插入对象
	 * 
	 * @param obj
	 */
	public void insert(Object obj) {
		this.getCurrentSession().save(obj);
	}

	/**
	 * 插入或者更新对象
	 * 
	 * @param obj
	 */
	public void insertOrUpdate(Object obj) {
		this.getCurrentSession().saveOrUpdate(obj);
	}

	/**
	 * 
	 * 根据ID,获得单一对象
	 * 
	 */
	public Object getObjectById(Class objectClass, Object id) {
		return this.getCurrentSession().get(objectClass, new Integer(id.toString()));
	}

	/**
	 * 
	 * 获得记录总数
	 * 
	 */
	public int getRecordCount(Class objectClass) {
		return getRecordCount(objectClass, null);
	}

	/**
	 * 
	 * 获得记录总数
	 * 
	 */
	public int getRecordCount(Class objectClass, List express) {
		// 获得一个session
		Criteria ca = this.getCurrentSession().createCriteria(objectClass);

		if (express != null && express.size() > 0) {
			for (int i = 0; i < express.size(); i++) {
				ca = ca.add((Criterion) express.get(i));
			}
		}

		try {
			return Integer.parseInt(ca.setProjection(Projections.rowCount()).uniqueResult().toString());
		} catch (Exception e) {
			return 0;
		}
	}

	public Object getUniqueResultByHql(String hql, List express) {
		Query ca = this.getCurrentSession().createQuery(hql);
		if (express != null) {
			for (int i = 0; i < express.size(); i++) {
				ca.setParameter(i, express.get(i));
			}
		}
		return ca.uniqueResult();
	}

	/**
	 * 
	 * 获得记录总数
	 * 
	 */
	public int getRecordCountBySql(String sql) {
		return getRecordCountBySql(sql, null);
	}

	/**
	 * 
	 * 获得记录总数
	 * 
	 */
	public int getRecordCountByHql(String sql) {
		return getRecordCountByHql(sql, null);
	}

	/**
	 * 
	 * 获得记录总数
	 * 
	 */
	public int getRecordCountBySql(String sql, List express) {
		SQLQuery ca = this.getCurrentSession().createSQLQuery(sql);
		if (express != null) {
			for (int i = 0; i < express.size(); i++) {
				ca.setParameter(i, express.get(i));
			}
		}
		try {
			return Integer.parseInt(ca.uniqueResult().toString());
		} catch (Exception e) {
			return 0;
		}
	}

	/**
	 * 
	 * 获得记录总数
	 * 
	 */
	public int getRecordCountByHql(String sql, List express) {
		Query ca = this.getCurrentSession().createQuery(sql);
		if (express != null) {
			for (int i = 0; i < express.size(); i++) {
				ca.setParameter(i, express.get(i));
			}
		}
		try {
			return Integer.parseInt(ca.uniqueResult().toString());
		} catch (Exception e) {
			return 0;
		}
	}

	/**
	 * 
	 * 执行sql
	 * 
	 */
	public int executeSql(String sql, List express) {
		// 获得一个session
		SQLQuery ca = this.getCurrentSession().createSQLQuery(sql);

		if (express != null) {
			for (int i = 0; i < express.size(); i++) {
				ca.setParameter(i, express.get(i));
			}
		}
		return ca.executeUpdate();
	}

	/**
	 * 
	 * 执行hql
	 * 
	 */
	public int executeHql(String hql, List express) {
		// 获得一个session
		Query ca = this.getCurrentSession().createQuery(hql);

		if (express != null) {
			for (int i = 0; i < express.size(); i++) {
				ca.setParameter(i, express.get(i));
			}
		}
		return ca.executeUpdate();
	}

	/**
	 * 
	 * 执行sql
	 * 
	 */
	public int executeSql(String sql) {
		return executeSql(sql, null);
	}

	/**
	 * 
	 * 执行hql
	 * 
	 */
	public int executeHql(String hql) {
		return executeHql(hql, null);
	}

	public static void main(String[] args) {
		int[] a = new int[1];
		a[0] = 111;

		System.out.println(a.length);
	}

	/**
	 * 根据一条sql，得到该条sql的汇总SQL
	 * 
	 * @param sql
	 * @return
	 * @throws SQLException
	 */

	public String getTotalSqlBySql(String sql, boolean flag) {
		if (flag) {
			return "select count(*) from ( " + sql + " ) a ";
		} else {
			int fromIndex = sql.toLowerCase().indexOf(" from ");
			return "select count(*) from " + sql.substring(fromIndex + 6);
		}
	}

	/**
	 * 根据正则表达式匹配模式，判断是否存在
	 * 
	 * @param s
	 * @param regEx
	 * @return
	 */
	public boolean isRegex(String s, String regEx) {
		try {
			java.util.regex.Pattern p = java.util.regex.Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
			java.util.regex.Matcher m = p.matcher(s);
			return m.find();
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}
	}

	public List getList(Class objectClass, List express, List order, int offset, int pageSize) {

		Criteria ca = this.getCurrentSession().createCriteria(objectClass);
		if (express != null && express.size() > 0) {
			for (int i = 0; i < express.size(); i++) {
				ca = ca.add((Criterion) express.get(i));
			}
		}
		// 加入排序条件

		if (order != null && order.size() > 0) {
			for (int i = 0; i < order.size(); i++) {
				ca = ca.addOrder((Order) (order.get(i)));
			}
		}
		// 取多少条记录
		ca.setMaxResults(pageSize);
		// 从哪儿开始取
		ca.setFirstResult(offset);
		return ca.list();
	}

	/**
	 * 不分页结果集
	 * 
	 * @param objectClass
	 * @param express
	 * @param order
	 * @return
	 */
	public List getList(Class objectClass, List express, List order) {

		Criteria ca = this.getCurrentSession().createCriteria(objectClass);
		if (express != null && express.size() > 0) {
			for (int i = 0; i < express.size(); i++) {
				ca = ca.add((Criterion) express.get(i));
			}
		}
		// 加入排序条件

		if (order != null && order.size() > 0) {
			for (int i = 0; i < order.size(); i++) {
				ca = ca.addOrder((Order) (order.get(i)));
			}
		}
		return ca.list();
	}
}
